<template>
<el-form>
    <el-form-item label="轮播图">
        <el-upload :show-file-list="false" :on-success="handleHotelPicSuccess" :before-upload="beforeHotelPicUpload" class="hotel-uploader" action="http://localhost:8081/hotel/ratation/addImg?module=ratation">
            <img v-if="url!=''" :src="url" />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
    </el-form-item>
</el-form>
</template>

<script>
export default {
    data() {
        return {
            url: "",
        };
    },
    methods: {
        handleHotelPicSuccess: function (res, file) {
            //跳转页面
            this.$message({
                message: "增加成功",
                type: "success",
            });
            setTimeout(() => {
                //需要延迟的代码 :3秒后延迟跳转到首页，可以加提示什么的
                this.$router.push("/hotel/ratationList");
                //延迟时间：3秒
            }, 3000);
            this.url = res.data.url;
            // 强制重新渲染
            this.$forceUpdate();
        },
        beforeHotelPicUpload: function (file) {
            const isJPG = file.type === "image/jpeg";
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isJPG) {
                this.$message.error("上传图片只能是 JPG 格式!");
            }
            if (!isLt2M) {
                this.$message.error("上传图片大小不能超过2MB!");
            }
            return isJPG && isLt2M;
        },
        //数据库中增加链接地址
        addUrl() {
            this.axios({
                method: "post",
                url: "http://localhost:8081/hotel/ratation/addImg",
            });
        },
    },
};
</script>

<style>
.hotel-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.hotel-uploader .el-upload:hover {
    border-color: #409eff;
}

.hotel-uploader .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.hotel-uploader img {
    width: 178px;
    height: 178px;
    display: block;
}
</style>
